#  generic_daemon.rb
#
#  This file is part of myvnet.
#
#  Copyright (C) 2008  Filipe David Borba Manana,  fdmanana@gmail.com
#
#  myvnet is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  myvnet is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with myvnet.  If not, see <http://www.gnu.org/licenses/>.


require 'lib/exceptions'
require 'lib/packet/injector'


module Daemon
  
  # Generic myvnet daemon.
  # Every myvnet daemon must subclass this class.
  # Not meant to be instantiated.
  #
  class GenericDaemon
    
    # Daemon's name. Used for debugging and logging.
    #
    attr_reader :name
    
    attr_reader :net_addresses
    
 
    # === Synopsis
    #
    #   Daemon::GenericDaemon.new(name, packet_injector, hwaddress [, net_address]*)
    #
    # === Args
    #
    # +name+::
    #   Name to assign to the daemon. This name is arbitrary and is specially
    #   useful for logging and debugging.
    #   
    # +packet_injector+::
    #   Instance of Packet::Injector. Used for injecting packets into the network.
    #   
    # +hwaddress+::
    #   Hardware address of the network interface through which packets are
    #   injected.
    #   
    # +net_addresses+::
    #   List of network addresses (IP addresses) of hosts that myvnet is 
    #   simulating. At least one network address must be provided.
    #
    # === Return
    #
    # A new myvnet daemon instance.
    #
    # === Description
    #
    # Instantiates a myvnet daemon.
    #
    def initialize(name, packet_injector, hwaddress, *net_addresses)      
      
      @packet_injector = packet_injector
      @hwaddress = hwaddress
      @name = name
      @net_addresses = net_addresses            
    end
    

    # === Synopsis
    #
    #   Daemon::GenericDaemon#process(packet)
    #
    # === Args
    #
    # +packet+::
    #   A packet captured from the network and belonging to the
    #   data link layer of the TCP/IP protocol suite. At the moment,
    #   only Ethernet is supported.
    #
    # === Return
    #
    # true if it processed the given packet, or false otherwise.
    #
    # === Description
    #
    # Processes a packet captured from the network.
    #
    def process(packet)      
    end
  
    
    private
    
    # === Synopsis
    #
    #   Daemon::GenericDaemon#inject(packet)
    #
    # === Args
    #
    # +packet+::
    #   An instance of Ethernet::Packet.
    #
    # === Return
    #
    # No value.
    #
    # === Description
    #
    # Injects a packet into the network.
    #
    def inject(packet)            
      @packet_injector.add_packet(packet, @name)
    end
      
  end

end
